package com.wlt.controller;

import com.wlt.pojo.CartGoods;
import com.wlt.result.BaseResult;
import com.wlt.service.CartService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 购物车
 */
@RestController
@RequestMapping("/user/cart")
public class CartController {
    @DubboReference
    private CartService cartService;
    
    /**
     * 根据用户id查询购物车
     * @param userId    用户id
     * @return          查询结果
     */
    @GetMapping("/findCartList")
    public BaseResult<List<CartGoods>> findCartList(@RequestHeader Long userId){
        List<CartGoods> cartList = cartService.findCartList(userId);
        
        return BaseResult.success(cartList);
    }
    
    /**
     * 新增商品
     * @param cartGoods 购物车商品
     * @param userId    令牌中携带的用户id
     * @return          添加结果
     */
    @PostMapping("/addCart")
    public BaseResult addCart (@RequestBody CartGoods cartGoods, @RequestHeader Long userId) {
        cartService.addCart(userId, cartGoods);
        
        return BaseResult.success();
    }
    
    /**
     * 需修改购物车
     * @param userId    令牌中携带的用户id
     * @param goodId    商品id
     * @param num       修改数量
     * @return          执行结果
     */
    @GetMapping("/handleCart")
    public BaseResult addCart (@RequestHeader Long userId, Long goodId, Integer num) {
        cartService.handleCart(userId, goodId, num);
        
        return BaseResult.success();
    }
    
    /**
     * 删除购物车商品
     * @param userId    用户id
     * @param goodId    商品id
     * @return          删除结果
     */
    @DeleteMapping("/delete")
    public BaseResult deleteCart (@RequestHeader Long userId, Long goodId) {
        cartService.deleteCart(userId, goodId);
        
        return BaseResult.success();
    }
}
